<template>
  <div style="min-width: 980px">
      <el-row>
        <el-col class="toLogin" :offset="6" :span="12" >
          <el-row>
            <el-col :span="12" >
            <!--     登录轮播       -->
                <el-carousel class="carousel" height="340px" indicator-position="none" arrow="never">
                  <el-carousel-item  v-for="(item,index) in loginImages" :key="index">
                    <el-image  class="carouselImg"
                              :src="item.imgUrl" fit="fill"></el-image>
                  </el-carousel-item>

                </el-carousel>
            </el-col>
            <!--     登录&注册表单    -->
            <el-col :span="12" >
              <el-form
                  style="padding:10px 20px 0 20px;"
                  :model="ruleForm"
                  label-position="top"
                  status-icon :rules="rules" ref="ruleForm"  class="demo-ruleForm">
                <span class="formTitle" >登录</span>
                <el-form-item label="手机号:" prop="phoneNumber" style="margin-top: 20px">
                  <el-input type="text"
                            maxLength="11"
                            placeholder="请输入手机号!"
                            v-model="ruleForm.phone" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码:" prop="passWord">
                  <el-input type="password"
                            placeholder="请输入密码!"
                            v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item >
                  <el-button style="width: 100%" type="primary" @click="submitForm('ruleForm')">登录/注册</el-button>
                  <span class="formBottomTip">提示:输入合法的手机号可直接登录!</span>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
  </div>
</template>

<script>
import {mapActions} from 'vuex'
export default {
  name: "ToLogin",
  data(){
    var validatePhone = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号'));
      } else {
        let regu = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$/;

        if (regu.test(value)){
          callback();
        }else{
          callback(new Error('请输入正确的手机号'));
        }
      }
    };
    var validatePass = (rule,value,callback) =>{
      if (value === ''){
        callback(new Error("请输入密码"));
      }else{
        if (value.length >= 6){
          callback();
        }else{
          callback(new Error('密码最小长度为6'));
        }
      }
    }
    return{
      loginImages:[
        {imgUrl:require('./img/img_1.jpg')},
        {imgUrl:require('./img/img_2.jpg')},
        {imgUrl:require('./img/img_3.jpg')},
      ],
      ruleForm: {
        phone: '',
        password: '',
      },
      rules: {
        phone: [
          { validator: validatePhone, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],

      }
    }
  },
  methods:{
    ...mapActions('LoginModule',['changeStatus']),
    //提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //调用login接口
          this.$api.login(this.ruleForm).then(
              (res)=>{
                if (res.code === 200){
                  this.$message.success("登录成功!");
                  //设置localStorage
                  localStorage.setItem('user',JSON.stringify(res.data));
                  //改变登录状态
                  this.changeStatus(true);

                  this.$router.push("/home")
                }else{
                  this.$message.error("登录失败!账号或密码错误!");
                }
              }
          )
        } else {
          this.$message.error("请先输入后在提交!");
          return false;
        }
      });
    }
  }
}
</script>

<style lang="scss" scoped>
  .toLogin{
    background:#e5e4e6;
    border-radius: 10px;
    margin-top: 100px;
    box-shadow: 1px 1px 6px;
    &:hover{
      box-shadow: 1px 1px 6px skyblue;
    }
    .carousel{
      overflow: hidden;
      border-radius: 10px 0 0 10px;
      .carouselImg{
        width: 100%;
        height: 400px;
      }

    }
    .formTitle{
      display: flex;
      justify-content: center;
      font-size: 25px;
    }
    .formBottomTip{
      display: flex;
      justify-content: center;
      font-size: 13px;
      color: #912c2c;
    }
  }

  /deep/ .el-form-item__label{
    margin-bottom: -15px;
    margin-top: -10px;
  }
</style>